1468M - Similar Sets - CodeForces Solution


data structures graphs implementation *2300

Please click on ads to support us..

C++ Code:

//region template
#include <bits/stdc++.h>

#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define rep(...)        GET_MACRO(__VA_ARGS__, repn, rep0)(__VA_ARGS__)
#define rep0(i, b)      for(int i = 0; i < (b); ++i)
#define repn(i, a, b)   for(int i = (a); i < (b); ++i)
#define trav(a, b)      for(auto& a : b)
#define all(a)          (a).begin(),(a).end()
#define endl            '\n'
#define sz(x)           ((int)(x).size())
#define tests           int ___;cin>>___;while(___--)

using namespace std;
using ll = int64_t;
using ld = long double;
using ul = uint64_t;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vpi = vector<pi>;

static constexpr int inf = (int) 1e9 + 5;
static constexpr ll infl = (ll) 1e18 + 5;
static mt19937 rng((uint32_t) chrono::duration_cast<chrono::nanoseconds>(
        chrono::high_resolution_clock::now().time_since_epoch()).count());
//endregion

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);

    tests {
        int n;
        cin >> n;
        vvi a(n);
        map<int, int> c;
        rep(i, n) {
            int k;cin >> k;
            rep(j, k) {
                int x;cin >> x;
                if (!c.count(x))
                    c.insert({x, sz(c)});
                a[i].push_back(c[x]);
            }
            sort(all(a[i]));
        }
        const int b = 250;

        vector p(sz(c), vpi());

        rep(i,n) {
            if (sz(a[i]) < b) continue;
            vi occ(sz(c), 0);
            trav(k, a[i]) occ[k] = 1;
            rep(j,n) {
                if (i == j) continue;
                int cnt = 0;
                trav(k, a[j]) {
                    cnt += occ[k];
                }
                if (cnt > 1) {
                    cout << i+1 << ' ' << j+1 << endl;
                    goto next;
                }
            }
        }

        rep(i,n) {
            if (sz(a[i]) >= b) continue;
            rep(j, sz(a[i])) {
                rep(k, j+1, sz(a[i])) {
                    p[a[i][j]].emplace_back(a[i][k], i);
                }
            }
        }
        rep(i,sz(c)) {
            sort(all(p[i]));
            rep(j, sz(p[i])-1) {
                if (p[i][j].first == p[i][j+1].first) {
                    cout << p[i][j].second + 1 << ' ' << p[i][j+1].second + 1 << endl;
                    goto next;
                }
            }
        }


        cout << -1 << endl;
        next:;
    };

    return 0;
}


Comments

Submit
0 Comments
More Questions

53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum